﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bundled.Entity;
using Bundled.ORM;
using System.Data.Objects;

namespace Bundled.Pages.Account
{
    public partial class Default : System.Web.UI.Page
    {
        int customerID;
        BundledDBEntities db;

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Session["Member"] == null)
            //    Response.Redirect("~/Pages/Register/");

            customerID = Convert.ToInt32(Session["ID"]);

            if (!IsPostBack)
            {
                using (db = new BundledDBEntities())
                {
                    // Populate state Drop Down Lists
                    List<State> states = db.States.ToList();
                    foreach (var state in states)
                    {
                        ListItem li = new ListItem();
                        li.Text = state.State1;
                        li.Value = state.State1;

                        ddlBillState.Items.Add(li);
                        ddlShipState.Items.Add(li);
                    }

                    // Populate Personal Data
                    ObjectResult<Get_PersonalInfo_Result> personalResult = db.Get_PersonalInfo(customerID);
                    List<Get_PersonalInfo_Result> personalResultList = personalResult.ToList();

                    if (personalResultList != null)
                    {
                        foreach (var result in personalResultList)
                        {
                            txtFName.Text = result.FName;
                            txtLName.Text = result.LName;
                            txtEmail.Text = result.Email;
                        }
                    }

                    // Populate Address Data
                    ObjectResult<Get_AddressInfo_Result> addressResult = db.Get_AddressInfo(customerID);
                    List<Get_AddressInfo_Result> addressResultList = addressResult.ToList();

                    if (addressResultList != null)
                    {
                        foreach (var result in addressResultList)
                        {
                            // Shipping
                            txtbxShipFirst.Text = result.Ship_FName != null ? result.Ship_FName : "";
                            txtbxShipLast.Text = result.Ship_LName != null ? result.Ship_LName : "";
                            txtbxShipAdd1.Text = result.Ship_Address1 != null ? result.Ship_Address1 : "";
                            txtbxShipAdd2.Text = result.Ship_Address2 != null ? result.Ship_Address2 : "";
                            txtShipCity.Text = result.Ship_City != null ? result.Ship_City : "";
                            ddlShipState.SelectedItem.Text = result.Ship_State != null ? result.Ship_State : "AL";
                            txtbxShipZip.Text = result.Ship_Zip != null ? result.Ship_Zip : "";

                            // Billing
                            txtbxBillFirst.Text = result.Bill_FName;
                            txtbxBillLast.Text = result.Bill_LName;
                            txtbxBillAdd1.Text = result.Bill_Address1;
                            txtbxBillAdd2.Text = result.Bill_Address2 != null ? result.Bill_Address2 : "";
                            txtBillCity.Text = result.Bill_City;
                            ddlBillState.SelectedValue = result.Bill_State;
                            txtbxBillZip.Text = result.Bill_Zip;                        
                        }
                    }            
                }

                if (Request.QueryString["display"] == "acct")
                    hfDisplay.Value = "acct";
                else if (Request.QueryString["display"] == "bill")
                    hfDisplay.Value = "bill";
            }
            if (chkbxShip.Checked)
                hfDisplay.Value = "bill";
        }

        protected void btnPersonalSave_Click(object sender, EventArgs e)
        {
            using (db = new BundledDBEntities())
            {
                Customer customer = db.Customers.SingleOrDefault(x => x.CustomerID == customerID);

                customer.FName = Utility.SanitizeText(txtFName.Text);
                customer.LName = Utility.SanitizeText(txtLName.Text);
                customer.Email = Utility.SanitizeText(txtEmail.Text);

                db.SaveChanges();
            }

            Response.Redirect("~/Pages/Account/Default.aspx?display=acct");
        }

        protected void btnPasswordSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                using (db = new BundledDBEntities())
                {
                    Customer customer = db.Customers.SingleOrDefault(x => x.CustomerID == customerID);

                    customer.Password = Utility.SanitizeText(txtNewPW.Text);

                    db.SaveChanges();
                    Response.Redirect("~/Pages/Account/Default.aspx?display=acct");
                }
            }
        }

        protected void cvOldPW_ServerValidate(object source, ServerValidateEventArgs args)
        {
            using (db = new BundledDBEntities())
            {
                Customer customer = db.Customers.SingleOrDefault(x => x.CustomerID == customerID);

                args.IsValid = false;
                if (args.Value == customer.Password)
                    args.IsValid = true;
                else
                    args.IsValid = false;
            }

            hfDisplay.Value = "acct";
        }

        protected void chkbxShip_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                txtbxBillFirst.Text = Utility.SanitizeText(txtbxShipFirst.Text);
                txtbxBillLast.Text = Utility.SanitizeText(txtbxShipLast.Text);
                txtbxBillAdd1.Text = Utility.SanitizeText(txtbxShipAdd1.Text);
                txtbxBillAdd2.Text = Utility.SanitizeText(txtbxShipAdd2.Text);
                txtBillCity.Text = Utility.SanitizeText(txtShipCity.Text);
                ddlBillState.SelectedValue = ddlShipState.SelectedValue;
                txtbxBillZip.Text = Utility.SanitizeText(txtbxShipZip.Text);
            }
            catch (Exception ex) { }
        }

        protected void btnShippingSave_Click(object sender, EventArgs e)
        {
            // Shipping Address
            if (Session["ID"] != null)
            {
                customerID = Convert.ToInt32(Session["ID"]);
                Address customerAddress;

                using (db = new BundledDBEntities())
                {
                    if (db.Addresses.Any(x => x.CustomerID == customerID))
                    {
                        // Update
                        customerAddress = db.Addresses.SingleOrDefault(x => x.CustomerID == customerID);

                        customerAddress.Ship_FName = Utility.SanitizeText(txtbxShipFirst.Text);
                        customerAddress.Ship_LName = Utility.SanitizeText(txtbxShipLast.Text);
                        customerAddress.Ship_Address1 = Utility.SanitizeText(txtbxShipAdd1.Text);
                        customerAddress.Ship_Address2 = Utility.SanitizeText(txtbxShipAdd2.Text);
                        customerAddress.Ship_City = Utility.SanitizeText(txtShipCity.Text);
                        customerAddress.Ship_State = ddlShipState.SelectedValue;
                        customerAddress.Ship_Zip = Utility.SanitizeText(txtbxShipZip.Text);

                        db.SaveChanges();
                    }
                    else
                    {
                        // Create New
                        customerAddress = new Address();

                        customerAddress.CustomerID = customerID;
                        customerAddress.Ship_FName = Utility.SanitizeText(txtbxShipFirst.Text);
                        customerAddress.Ship_LName = Utility.SanitizeText(txtbxShipLast.Text);
                        customerAddress.Ship_Address1 = Utility.SanitizeText(txtbxShipAdd1.Text);
                        customerAddress.Ship_Address2 = Utility.SanitizeText(txtbxShipAdd2.Text);
                        customerAddress.Ship_City = Utility.SanitizeText(txtShipCity.Text);
                        customerAddress.Ship_State = ddlShipState.SelectedValue;
                        customerAddress.Ship_Zip = Utility.SanitizeText(txtbxShipZip.Text);

                        db.Addresses.Add(customerAddress);
                        db.SaveChanges();
                    }
                }
                Response.Redirect("~/Pages/Account/Default.aspx?display=bill");
            }
        }

        protected void btnBillingSave_Click(object sender, EventArgs e)
        {
            // Billing Address
            if (Session["ID"] != null)
            {
                customerID = Convert.ToInt32(Session["ID"]);
                Address customerAddress;

                using (db = new BundledDBEntities())
                {
                    if (db.Addresses.Any(x => x.CustomerID == customerID))
                    {
                        // Update
                        customerAddress = db.Addresses.SingleOrDefault(x => x.CustomerID == customerID);

                        customerAddress.Bill_FName = Utility.SanitizeText(txtbxBillFirst.Text);
                        customerAddress.Bill_LName = Utility.SanitizeText(txtbxBillLast.Text);
                        customerAddress.Bill_Address1 = Utility.SanitizeText(txtbxBillAdd1.Text);
                        customerAddress.Bill_Address2 = Utility.SanitizeText(txtbxBillAdd2.Text);
                        customerAddress.Bill_City = Utility.SanitizeText(txtBillCity.Text);
                        customerAddress.Bill_State = ddlBillState.SelectedValue;
                        customerAddress.Bill_Zip = Utility.SanitizeText(txtbxBillZip.Text);

                        db.SaveChanges();
                    }
                    else
                    {
                        // Create New
                        customerAddress = new Address();

                        customerAddress.CustomerID = customerID;
                        customerAddress.Bill_FName = Utility.SanitizeText(txtbxBillFirst.Text);
                        customerAddress.Bill_LName = Utility.SanitizeText(txtbxBillLast.Text);
                        customerAddress.Bill_Address1 = Utility.SanitizeText(txtbxBillAdd1.Text);
                        customerAddress.Bill_Address2 = Utility.SanitizeText(txtbxBillAdd2.Text);
                        customerAddress.Bill_City = Utility.SanitizeText(txtBillCity.Text);
                        customerAddress.Bill_State = ddlBillState.SelectedValue;
                        customerAddress.Bill_Zip = Utility.SanitizeText(txtbxBillZip.Text);

                        db.Addresses.Add(customerAddress);
                        db.SaveChanges();
                    }
                }

                Response.Redirect("~/Pages/Account/Default.aspx?display=bill");
            }
        }       
    }
}